package com.jingyong.infosite.service;

import com.jingyong.infosite.dto.response.LoanWikiResponse;
import com.mongodb.BasicDBObject;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import org.bson.Document;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Hill Dong
 * @date 2018/1/23
 */
@Service
public class LoanWikiService {
	private static final String LOAN_WIKI_DATABASE_NAME = "baike";
	Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private MongoTemplate mongoTemplate;

	public LoanWikiResponse getLoanWikiResponse() {
		Object loanWords = getLoanWikiByType("net_loan_words", 6);
		Object loanIntroductions = getLoanWikiByType("net_loan_introduction", 5);
		Object loanEventBrief = getLoanWikiByType("net_event_brief", 3);
		Object hots_words_brief = getLoanWikiByType("hots_words_brief", 5);
		LoanWikiResponse response = new LoanWikiResponse();
		response.setLoanWords(loanWords);
		response.setLoanIntroductions(loanIntroductions);
		response.setLoanEventBrief(loanEventBrief);
		response.setHotWordBrief(hots_words_brief);
		return response;
	}

	public Object getLoanWikiByType(String type, int limit) {
		try {
			MongoCollection<Document> coll = mongoTemplate.getCollection(LOAN_WIKI_DATABASE_NAME);
			BasicDBObject filter = new BasicDBObject();
			filter.put("baike_type", type);
			FindIterable<Document> iterable = coll.find(filter).limit(limit).sort(new BasicDBObject("create_time", -1));
			MongoCursor<Document> cursor = iterable.iterator();
			List<Document> list = new ArrayList<>();
			while (cursor.hasNext()) {
				list.add(cursor.next());
			}
			return list;
		} catch (Exception e) {
			logger.error("get wiki error, the type is {}", type);
			return null;
		}
	}

	public Object getLoanWikiByTitle(String type, String title) {
		try {
			MongoCollection<Document> coll = mongoTemplate.getCollection(LOAN_WIKI_DATABASE_NAME);
			BasicDBObject filter = new BasicDBObject();
			filter.put("baike_type", type);
			filter.put("title", title);
			FindIterable<Document> iterable = coll.find(filter).limit(1).sort(new BasicDBObject("create_time", -1));
			MongoCursor<Document> cursor = iterable.iterator();
			Document doc = null;
			if (cursor.hasNext()) {
				doc = cursor.next();
			}
			return doc;
		} catch (Exception e) {
			logger.error("fail to get wiki by title, the title is {}", title);
			return null;
		}
	}

	public Object getLoanWikiById(String id) {
		try {
			MongoCollection<Document> coll = mongoTemplate.getCollection(LOAN_WIKI_DATABASE_NAME);
			BasicDBObject filter = new BasicDBObject();
			filter.put("_id", id);
			FindIterable<Document> iterable = coll.find(filter);
			MongoCursor<Document> cursor = iterable.iterator();
			Document doc = null;
			if (cursor.hasNext()) {
				doc = cursor.next();
			}
			return doc;
		} catch (Exception e) {
			logger.error("fail to get wiki by id, the id is {}", id);
			return null;
		}
	}

}
